home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / hplip / levels.py < prev    next >
Text File  |  2009-10-09  |  7KB  |  208 lines

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # (c) Copyright 2003-2008 Hewlett-Packard Development Company, L.P.
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  19. #
  20. # Author: Don Welch
  21. #
  22.  
  23. __version__ = '2.0'
  24. __title__ = 'Supply Levels Utility'
  25. __mod__ = 'hp-levels'
  26. __doc__ = "Display bar graphs of current supply levels for supported HPLIP printers."
  27.  
  28. # Std Lib
  29. import sys
  30. import getopt
  31. import time
  32. import operator
  33. import os
  34.  
  35. # Local
  36. from base.g import *
  37. from base import device, status, utils, tui, module
  38. from prnt import cups
  39.  
  40. DEFAULT_BAR_GRAPH_SIZE = 8*(tui.ttysize()[1])/10
  41.  
  42.  
  43. def logBarGraph(agent_level, agent_type, size=DEFAULT_BAR_GRAPH_SIZE, use_colors=True, bar_char='/'):
  44.     #print agent_level, agent_type, size, use_colors, bar_char
  45.  
  46.     adj = 100.0/size
  47.     if adj==0.0: adj=100.0
  48.     bar = int(agent_level/adj)
  49.     if bar > (size-2): bar = size-2
  50.  
  51.     if use_colors:
  52.         if agent_type in (AGENT_TYPE_CMY, AGENT_TYPE_KCM, AGENT_TYPE_CYAN, AGENT_TYPE_CYAN_LOW):
  53.             log.info(log.codes['teal'])
  54.         elif agent_type in (AGENT_TYPE_MAGENTA, AGENT_TYPE_MAGENTA_LOW):
  55.             log.info(log.codes['fuscia'])
  56.         elif agent_type in (AGENT_TYPE_YELLOW, AGENT_TYPE_YELLOW_LOW):
  57.             log.info(log.codes['yellow'])
  58.         elif agent_type == AGENT_TYPE_BLUE:
  59.             log.info(log.codes['blue'])
  60.         elif agent_type == AGENT_TYPE_BLACK:
  61.             log.info(log.codes['bold'])
  62.         elif agent_type in (AGENT_TYPE_LG, AGENT_TYPE_G, AGENT_TYPE_PG):
  63.             pass
  64.  
  65.     color = ''
  66.     if use_colors:
  67.         if agent_type in (AGENT_TYPE_CMY, AGENT_TYPE_KCM):
  68.             color = log.codes['fuscia']
  69.  
  70.     log.info(("-"*size)+color)
  71.  
  72.     color = ''
  73.     if use_colors:
  74.         if agent_type in (AGENT_TYPE_CMY, AGENT_TYPE_KCM):
  75.             color = log.codes['yellow']
  76.  
  77.     log.info("%s%s%s%s (approx. %d%%)%s" % ("|", bar_char*bar,
  78.              " "*(size-bar-2), "|", agent_level, color))
  79.  
  80.     color = ''
  81.     if use_colors:
  82.         color = log.codes['reset']
  83.  
  84.     log.info(("-"*size)+color)
  85.  
  86.  
  87.  
  88. log.set_module('hp-levels')
  89.  
  90. try:
  91.     mod = module.Module(__mod__, __title__, __version__, __doc__, None,
  92.                         (INTERACTIVE_MODE,))
  93.  
  94.     mod.setUsage(module.USAGE_FLAG_DEVICE_ARGS,
  95.         extra_options=[
  96.         ("Bar graph size:", "-s<size> or --size=<size> (current default=%d)" % DEFAULT_BAR_GRAPH_SIZE, "option", False),
  97.         ("Use colored bar graphs:", "-c or --color (default is colorized)", "option", False),
  98.         ("Bar graph character:", "-a<char> or --char=<char> (default is '/')", "option", False)])
  99.  
  100.  
  101.     opts, device_uri, printer_name, mode, ui_toolkit, lang = \
  102.         mod.parseStdOpts('s:ca:', ['size=', 'color', 'char='])
  103.  
  104.     device_uri = mod.getDeviceUri(device_uri, printer_name)
  105.  
  106.     size = DEFAULT_BAR_GRAPH_SIZE
  107.     color = True
  108.     bar_char = '/'
  109.  
  110.     for o, a in opts:
  111.         if o in ('-s', '--size'):
  112.             try:
  113.                 size = int(a.strip())
  114.             except (TypeError, ValueError):
  115.                 log.warn("Invalid size specified. Using the default of %d" % DEFAULT_BAR_GRAPH_SIZE)
  116.                 size = DEFAULT_BAR_GRAPH_SIZE
  117.  
  118.             if size < 1 or size > DEFAULT_BAR_GRAPH_SIZE:
  119.                 log.warn("Invalid size specified. Using the default of %d" % DEFAULT_BAR_GRAPH_SIZE)
  120.                 size = DEFAULT_BAR_GRAPH_SIZE
  121.  
  122.         elif o in ('-c', '--color'):
  123.             color = True
  124.  
  125.         elif o in ('-a', '--char'):
  126.             try:
  127.                 bar_char = a[0]
  128.             except KeyError:
  129.                 bar_char = '/'
  130.  
  131.  
  132.     try:
  133.         d = device.Device(device_uri, printer_name)
  134.     except Error:
  135.         log.error("Error opening device. Exiting.")
  136.         sys.exit(1)
  137.  
  138.     try:
  139.         try:
  140.             d.open()
  141.             d.queryDevice()
  142.         except Error, e:
  143.             log.error("Error opening device (%s). Exiting." % e.msg)
  144.             sys.exit(1)
  145.  
  146.         if d.mq['status-type'] != STATUS_TYPE_NONE:
  147.             log.info("")
  148.  
  149.             sorted_supplies = []
  150.             a = 1
  151.             while True:
  152.                 try:
  153.                     agent_type = int(d.dq['agent%d-type' % a])
  154.                     agent_kind = int(d.dq['agent%d-kind' % a])
  155.                     agent_sku = d.dq['agent%d-sku' % a]
  156.                     log.debug("%d: agent_type %d agent_kind %d agent_sku '%s'" % (a, agent_type, agent_kind, agent_sku))
  157.                 except KeyError:
  158.                     break
  159.                 else:
  160.                     sorted_supplies.append((a, agent_kind, agent_type, agent_sku))
  161.                 a += 1
  162.  
  163.             sorted_supplies.sort(lambda x, y: cmp(x[1], y[1]) or cmp(x[3], y[3]))
  164.  
  165.             for x in sorted_supplies:
  166.                 a, agent_kind, agent_type, agent_sku = x
  167.                 agent_health = d.dq['agent%d-health' % a]
  168.                 agent_level = d.dq['agent%d-level' % a]
  169.                 agent_desc = d.dq['agent%d-desc' % a]
  170.                 agent_health_desc = d.dq['agent%d-health-desc' % a]
  171.  
  172.                 if agent_health in (AGENT_HEALTH_OK, AGENT_HEALTH_UNKNOWN) and \
  173.                     agent_kind in (AGENT_KIND_SUPPLY,
  174.                                     AGENT_KIND_HEAD_AND_SUPPLY,
  175.                                     AGENT_KIND_TONER_CARTRIDGE,
  176.                                     AGENT_KIND_MAINT_KIT,
  177.                                     AGENT_KIND_ADF_KIT,
  178.                                     AGENT_KIND_INT_BATTERY,
  179.                                     AGENT_KIND_DRUM_KIT,):
  180.  
  181.                     log.info(log.bold(agent_desc))
  182.                     log.info("Part No.: %s" % agent_sku)
  183.                     log.info("Health: %s" % agent_health_desc)
  184.                     logBarGraph(agent_level, agent_type, size, color, bar_char)
  185.                     log.info("")
  186.  
  187.                 else:
  188.                     log.info(log.bold(agent_desc))
  189.                     log.info("Part No.: %s" % agent_sku)
  190.                     log.info("Health: %s" % agent_health_desc)
  191.                     log.info("")
  192.  
  193.  
  194.         else:
  195.             log.error("Status not supported for selected device.")
  196.             sys.exit(1)
  197.     finally:
  198.         d.close()
  199.  
  200. except KeyboardInterrupt:
  201.     log.error("User exit")
  202.  
  203. log.info("")
  204. log.info("Done.")
  205.  
  206.  
  207.  
  208.